package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.advice.OneAnnotation;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryPaeam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzService clazzService;

//    @Override
//    public PageResult page(StudentQueryPaeam studentQueryPaeam) {
//        PageHelper.startPage(studentQueryPaeam.getPage(), studentQueryPaeam.getPageSize());
//
//        Page<Student> studentList = (Page<Student>) studentMapper.page(studentQueryPaeam);
//        long total = studentList.getTotal();
//        List<Student> rows = studentList.getResult();
//
//        return new PageResult<>(total, rows);
//    }

    @Override
    public PageResult page(StudentQueryPaeam studentQueryPaeam) {
        IPage<Student> page = new Page<>(studentQueryPaeam.getPage(), studentQueryPaeam.getPageSize());
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(studentQueryPaeam.getName() != null, Student::getName, studentQueryPaeam.getName());
        queryWrapper.eq(studentQueryPaeam.getDegree() != null, Student::getDegree, studentQueryPaeam.getDegree());
        queryWrapper.eq(studentQueryPaeam.getClazzId() != null, Student::getClazzId, studentQueryPaeam.getClazzId());
        page = page(page, queryWrapper);
        List<Student> studentList = page.getRecords();
        studentList.forEach(student -> {
            if (student.getClazzId() != null) {
                Clazz clazz = clazzService.getById(student.getClazzId());
                student.setClazzName(clazz.getName());
            }
        });
        return new PageResult<>(page.getTotal(),page.getRecords());
    }

//    @Override
//    public void delete(List<Integer> ids) {
//        studentMapper.delete(ids);
//    }
//
//    @Override
//    public void add(Student student) {
//        student.setUpdateTime(LocalDateTime.now());
//        student.setCreateTime(LocalDateTime.now());
//        studentMapper.add(student);
//    }
//
//    @Override
//    public Student getById(Integer id) {
//        return studentMapper.getById(id);
//    }
//
//    @Override
//    public void update(Student student) {
//        student.setUpdateTime(LocalDateTime.now());
//        studentMapper.update(student);
//    }
//
    @Override
    public void score(Integer id, Integer score) {
        studentMapper.score(id, score);
    }

    @Override
    public void saveStu(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }
}
